package com.fable.hardware.temperature;

import android.util.Log;

import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.ip.IpParameters;
import com.serotonin.modbus4j.msg.*;
import com.serotonin.util.queue.ByteQueue;

/**
 * Modbus Tcp工具类
 *
 * @author afeey
 */
public class ModbusTcpUtil {

    protected static final String LOGGER_TAG = ModbusTcpUtil.class.getName();

    /**
     * 写寄存器
     *
     * @param ip      ip
     * @param port    端口
     * @param slaveId 从机ID
     * @param start   起始地址
     * @param values  值
     */
    public static void writeRegisters(String ip, int port, int slaveId, int start, short[] values) {
        ModbusFactory modbusFactory = new ModbusFactory();
        IpParameters params = new IpParameters();
        params.setHost(ip);
        if (port > 0) {
            params.setPort(port);
        }

        ModbusMaster master = null;
        master = modbusFactory.createTcpMaster(params, true);
        try {
            master.init();
            Log.d(LOGGER_TAG, "master init success");
        } catch (ModbusInitException e) {
            Log.d(LOGGER_TAG, "master init fail");
            Log.d(LOGGER_TAG, e.getStackTrace().toString());
        }
        try {
            WriteRegistersRequest request = new WriteRegistersRequest(slaveId, start, values);
            WriteRegistersResponse response = (WriteRegistersResponse) master.send(request);
            if (response.isException()) {
                Log.d(LOGGER_TAG, "exception: message=" + response.getExceptionMessage());
            } else {
                Log.d(LOGGER_TAG, "success");
            }
        } catch (ModbusTransportException e) {
            Log.d(LOGGER_TAG, e.getStackTrace().toString());
        }
    }

    /**
     * 读取寄存器
     *
     * @param ip        ip
     * @param port      端口
     * @param slaveId   从机ID
     * @param start     起始地址
     * @param readLenth 读取长度
     * @return 字节队列
     */
    public static ByteQueue readHoldingRegisters(String ip, int port, int slaveId, int start, int readLenth) {
        ModbusFactory modbusFactory = new ModbusFactory();
        IpParameters params = new IpParameters();
        params.setHost(ip);
        if (port > 0) {
            params.setPort(port);
        }
        ModbusMaster master = null;
        master = modbusFactory.createTcpMaster(params, false);
        try {
            master.init();
            Log.d(LOGGER_TAG, "master init success");
        } catch (ModbusInitException e) {
            Log.d(LOGGER_TAG, "master init fail");
            Log.d(LOGGER_TAG, e.getMessage());
        }
        ModbusRequest request = null;
        try {
            request = new ReadHoldingRegistersRequest(slaveId, start, readLenth);
        } catch (ModbusTransportException e) {
            Log.d(LOGGER_TAG, "master init fail");
            Log.d(LOGGER_TAG, e.getMessage());
        }
        ModbusResponse response = null;
        try {
            response = master.send(request);
        } catch (ModbusTransportException e) {
            Log.d(LOGGER_TAG, e.getMessage());
        }
        ByteQueue byteQueue = new ByteQueue();
        response.write(byteQueue);
        return byteQueue;
    }
}
